using System;
using System.Collections.Generic;

namespace Client.Hotfix
{
    [UGFM(UGFMWeight.GameModule)]
    public class GameModule : IAwake, IDestroy
    {
        private Dictionary<Type, BaseModule> dicts = null;
        private BaseModule curModule;
        public void Awake()
        {
            dicts = new();
            AddModule(new LoginModule(this));

        }

        public void Destroy()
        {
            dicts?.Clear();
            dicts = null;
        }

        public void EnterModule<T>() where T : BaseModule
        {
            if (dicts.TryGetValue(typeof(T), out var module))
            {
                curModule?.OnExit();
                curModule = module;
                curModule.OnEnter();
            }
        }

        private void AddModule(BaseModule instance)
        {
            dicts.TryAdd(instance.GetType(), instance);
        }
    }

}
